﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Common;
using System.Collections.Generic;

namespace Job.Web.userControls.Admin
{
    public partial class LevelDictionaryInfo : BaseCtrl
    {
        DictionaryLogic logic = new DictionaryLogic();

        private int dictClass
        {
            get { return myR["dictclass"].IntValue; }
        }
        public string fatherCode
        {
            get
            {
                string father = myR["father"].StringValue;
                father = (null == father) ? "" : father.Trim();
                //if (father.Length == 0) father = Constants.EMPTY_FATHER;
                return father;
            }
        }
        public int id
        {
            get { return myR["id"].IntValue; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!LoadData())
                {
                    tbCode.Enabled = false;
                    tbName.Enabled = false;
                    rblStatus.Enabled = false;
                    btSave.Enabled = false;
                }
            }
        }

        private bool LoadData()
        {
            if (id > 0)
            {
                try
                {
                    JobLevelDictionary dict = logic.SelectLevel(id);
                    if (dict == null) return false;
                    if (!FillFather(dict.FatherCode, dict.DictClass)) return false;

                    this.tbCode.MaxLength = 4;
                    this.tbCode.Text = dict.SubCode;
                    this.tbCode.Enabled = false;

                    this.tbName.Text = dict.DictName;
                    if (dict.DictState.Value)
                        this.rblStatus.Items[0].Selected = true;
                    else
                        this.rblStatus.Items[1].Selected = true;
                    this.hidId.Value = dict.DictId.ToString();
                    this.hidClass.Value = dict.DictClass;

                    return true;
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex.ToString());
                    return false;
                }

            }
            else
            {
                hidClass.Value = Constants.Num2FixLenStr(dictClass, Constants.DICT_CLASS_LEN);
                hidId.Value = "0";
                hidFather.Value = fatherCode;
                tbName.Text = "";
                tbCode.MaxLength = 2;
                tbCode.Text = "";
                rblStatus.Items[0].Selected = true;
                
                string strClass = Constants.Num2FixLenStr(dictClass, Constants.DICT_CLASS_LEN);
                return this.FillFather(this.fatherCode,strClass);
            }

        }

        private bool FillFather(string father,string type)
        {
            father = (null == father) ? "" : father.Trim();
            if (father.Equals(""))
            {
                this.lbFather.Text = "";
                this.lbFather.Visible = false;
                this.hidFather.Value = Constants.EMPTY_FATHER;
                return true;
            }
            IList<JobLevelDictionary> fathers = logic.ListBySubCodeAndClass(father,type);
            if (null == fathers || fathers.Count == 0)
            {
                this.lbFather.Text = "";
                this.lbFather.Visible = false;
                this.hidFather.Value = Constants.EMPTY_FATHER;
                return false;
            }
            else
            {
                JobLevelDictionary fatherDict = fathers[0];
                this.lbFather.Text = "(" + fatherDict.SubCode + ") " + fatherDict.DictName;
                this.lbFather.Visible = true;
                this.hidFather.Value = father;
                return true;
            }
        }

        protected void btReturn_Click(object sender, EventArgs e)
        {
            string url = "AdminMain.aspx?controlType=" + dictClass.ToString() + "&dictclass=" + dictClass.ToString();
            Response.Redirect(url);
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            JobLevelDictionary dict = new JobLevelDictionary();
            dict.DictId = Convert.ToInt32(hidId.Value);
            dict.DictClass = hidClass.Value;
            dict.FatherCode = hidFather.Value;
            dict.DictName = tbName.Text.Trim();
            if (rblStatus.Items[0].Selected)
                dict.DictState = true;
            else
                dict.DictState = false;

            String code = tbCode.Text.Trim();
            if (dict.DictId == 0)
            {   
                if (code.Length != 2)
                {
                    GotoFailure("输入的配置项代码必须为两位！");
                    return;
                }
                //检查配置项代码是否存在
                string subCode = hidFather.Value + code;
                subCode = subCode.Trim();

                IList<JobLevelDictionary> results = logic.ListBySubCodeAndClass(subCode, hidClass.Value);
                if (null != results && results.Count > 0)
                {
                    GotoFailure("指定的子配置项代码已存在！");
                    return;
                }

                dict.SubCode = subCode;
                logic.InsertLevel(dict);
            }
            else
            {
                dict.SubCode = code;
                logic.UpdateLevel(dict);
            }

            switch (this.dictClass)
            {
                case (int)Constants.DictionaryType.ADDR_CITY:
                    MemoryDictionary.LoadAddrDictionary();
                    break;
                case (int)Constants.DictionaryType.INDUSTRY_CLASS:
                    MemoryDictionary.LoadIndustryDictionary();
                    break;
                case (int)Constants.DictionaryType.POSITION_CLASS:
                    MemoryDictionary.LoadPositionClassDictionary();
                    break;
                case (int)Constants.DictionaryType.SPECIALTY_CLASS:
                    MemoryDictionary.LoadSpecialtyDictionary();
                    break;
            }

            string message = Server.UrlEncode(string.Format("保存配置项成功！"));
            string urlPath = Server.UrlEncode("/AdminWeb/AdminMain.aspx?father=" + dict.FatherCode
                + "&controlType=" + dictClass.ToString() + "&dictclass=" + dictClass.ToString());
            string url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
            Response.Redirect(url);

        }

        private void GotoFailure(string message)
        {
            message = Server.UrlEncode(message);
            string urlPath = Server.UrlEncode("/AdminWeb/AdminMain.aspx?controlType="
                + dictClass.ToString() + "&dictclass=" + dictClass.ToString()) + "&father=" + fatherCode;
            string url = string.Format("~/ErrorMessage.aspx?flag=1&message={0}&returnURL={1}", message, urlPath);
            Response.Redirect(url);
        }

    }
}